package rickyGame.ui.login
{
	import com.greensock.TweenMax;
	
	import comp.Button;
	import comp.DropDownList;
	import comp.InputText;
	import comp.Label;
	import comp.Panel;
	import comp.TabBar;
	import comp.TextArea;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	
	import ricky.animate.AnimSprite;
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	import ricky.game.graphic.Graphic;
	
	import rickyGame.game.Player;
	import rickyGame.game.action.AnimaAct;
	import rickyGame.game.graphic.DefaultScence;
	import rickyGame.game.graphic.PlayerGraphic;
	import rickyGame.model.*;
	import rickyGame.model.config.*;
	import rickyGame.ui.BaseView;
	import rickyGame.utils.ResURL;

	/**
	 * <h1>登陆UI</h1>
	 * @author:pu
	 * @data:2010.9.15
	 */
	public class LoginView extends BaseView
	{
		public function LoginView()
		{
			super();
			this.width=1000;
			this.height=700;
			this.useTheme=false;
			this.head="none";
			allRoles=new Dictionary();
		}
		
		//override
		override protected function createChildren():void
		{
			_textArea=new TextArea();
			_textArea.htmlText=CLocale.getValue("createHeroInfo");
			_textArea.width=200;	
			_textArea.height=100;
			_textArea.x=100;
			_textArea.y=100;
			
			input=new InputText();
			input.text="玩家"+new Date().minutes;
			input.width=100;
			input.x=_textArea.x;
			input.y=_textArea.y+110;
			this.addChild(input);
			
			
			var allow:Array=CLocale.getValue("allowRole").toString().split(",");
			
			var allowData:Array=[];
			var hero:Unit;
			for each(var v:String in allow)
			{
				hero=ConfigUnitHelper.getHeroConfig(v);
				allowData.push(hero);
			}
			
//			var label:Label=new Label();
//			label.text="请选择下面的角色";
//			label.x=400;
//			label.y=130;
			
//			act=new TabBar();
//			act.showField="name";
//			act.enabled=false;
//			act.data=[{"name":"站立","act":"stand"},{"name":"跑动","act":"run"},{"name":"攻击","act":"attack"},
//				{"name":"魔法","act":"beAttack"},{"name":"死亡","act":"death"}];
			
			
//			act.clickFunction=function(e:Event):void
//			{
//				if(_choose)
//					_choose.addAct(new AnimaAct(_choose,act.selectedValue.act));
//			}
//			act.x=400;
//			act.y=100;
			
			
			
			
//			var roles:DropDownList=new DropDownList();
//			roles.showField="name";
//			roles.selectedIndex=0;
//			roles.x=650;
//			roles.y=100;
//			roles.data=[{"name":"人类","data":"human"},{"name":"恶魔","data":"demon"},{"name":"兽镞","data":"animal"}];
//			roles.clickFunction=function(e:Event):void
//			{
//				_rolePanel.removeAllChild();
//				var tp:Player,i:int=0;
//				var btn:Button;
//				for each(var herou:HeroUnit in arr)
//				{
//					if(herou.race==roles.selectedValue.data)
//					{
//						btn=new Button();
//						btn.text=herou.name;
//						btn.tips=herou.tips+":</br>"+herou.revivetip;
//						btn.tag=i;
//						btn.clickFunction=onroleChoose;
//						btn.initialize();
//						tp=new Player();
//						tp.graphic.defaultScene=DefaultScence.deafultAnimScene;
//						tp.load(herou);
//						tp.wear();
//						tp.direction=3;
//						tp.playAnimAct("stand");
//						tp.active=true;
//						
//						tp.x=i*70;
//						tp.y=180;
//						
//						btn.x=tp.x-10;
//						btn.y=tp.y-120;
//						tp.updatepostion();
//						_rolePanel.addChild(tp.graphic);
//						_rolePanel.addChild(btn);
//						allRoles[i]=tp;
//						i++;
//						
//					}
//				}
//			}
//				
//				this.addChild(roles);
//				
			_rolePanel=new Panel();
			_rolePanel.backgroundImageX=0;
			_rolePanel.backgroundImageY=0
			_rolePanel.head="none";
//			_rolePanel.gap=20;
			_rolePanel.useTheme=false;
			_rolePanel.width=650;
			_rolePanel.y=250;
			_rolePanel.layout="tileLayout";
			_rolePanel.tileWidth=120;
			_rolePanel.titleHeight=200;
			_rolePanel.x=300;
			_rolePanel.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			_rolePanel.addEventListener(MouseEvent.MOUSE_OUT,onMoveOut);
			_rolePanel.addEventListener(MouseEvent.CLICK,onClick);
//			
			if(allowData){
				var tp:Player,i:int=0;
//				var btn:Button;
				for each(var herou:Unit in allowData)
				{

						tp=new Player();
						PlayerGraphic(tp.graphic).defaultScene=DefaultScence.deafultAnimScene;
						tp.load(herou);
						tp.wear();
						tp.direction=3;
						tp.playAnimAct("stand");
						tp.active=true;

//						tp.updatepostion();
						_rolePanel.addChild(tp.graphic as DisplayObject);
						allRoles[i]=tp;
				}
			}
//			
//			var dif:BitmapData=new BitmapData(100,100,true,0x00);
//			var tips:TextField=new TextField();
//			tips.text="角色资源加载中..";
//			dif.draw(tips);
//			
			btnbegin=new Button();
			btnbegin.text="开始游戏";
			btnbegin.color=0xff0000;
			btnbegin.fontSize=14;
			btnbegin.y=500;
			btnbegin.x=400;
			btnbegin.clickFunction=beginGame;
			btnbegin.initialize();
			
//			
//			
//			l=new Label();
//			l.name="selected";
//			l.text="↓↓";
//			l.visible=false;			
				
			this.addChild(_textArea);
			this.addChild(_rolePanel)
//			this.addChild(label);
//			this.addChild(act);
			this.addChild(btnbegin);
			btnbegin.enabled=false;
//			this.addChild(l);
		}
		
		protected function onClick(e:MouseEvent):void
		{
//			var p:Point=new Point(e.stageX,e.stageY);
//			var arr:Array=_rolePanel.getObjectsUnderPoint(p);
//			
			var obj:Array=new Array();
			var ani:AnimSprite;
			var color:uint;
			var arr:Array=[];
			var local:Point=new Point(e.localX,e.localY);
			var global1:Point=new Point(e.stageX,e.stageY);
			var itemLocal:Point;
			
			arr=this._rolePanel.getObjectsUnderPoint(global1);
//			obj.push(this._rolePanel.globalToLocal(global1));
			for each(var i:* in arr)
			{
				if(i is AnimSprite)
				{
					itemLocal=i.globalToLocal(global1);
					color=i.bitmapData.getPixel(itemLocal.x,itemLocal.y);
					if(color>0 && i.parent is Graphic)
						obj.push(i.parent.actor);
				}
			}
//			var _choose:Pu;
			if(obj.length>0)
			{
				if(_lastselected)
				{
					PlayerGraphic(_lastselected.graphic).filters=[];
					_lastselected.playAnimAct("stand");
				}
				
				btnbegin.enabled=true;
				_choose=obj[0];
				PlayerGraphic(_choose.graphic).filters=[new GlowFilter(0xffff00,.8)];
				_choose.playAnimAct("attack");
				_lastselected=_choose;
			}
			
		}
	
//		protected function onroleChoose(e:MouseEvent):void
//		{
//			var btn1:Button=e.target as Button;
//			var i:int=int(btn1.tag);
//			btnbegin.enabled=true;
//			if(_choose)
//			{
//				_choose.graphic.filters=[];
//				_choose.playAnimAct("stand");
//			}
//			_choose=allRoles[i];
//			_choose.direction=4;
//			_choose.graphic.filters=[new GlowFilter(0xffff00,.8)];
//			_choose.playAnimAct("attack");
//			_chooseRole=i+"";
//		}
		
		protected function beginGame(e:Event):void
		{
			this.dispatchEvent(new Event("createSure"));
		}
		
		protected function onMouseOver(e:MouseEvent):void
		{
			if(e.target is Sprite)
			{
//				TweenMax.to(e.target,0.2,{glowFilter:{color:0xffffff,blurX:5,blurY:5,alpha:1,strength:2,quality:1}});
			}
		}
		
		
		protected function onMoveOut(e:Event):void
		{
			if(e.target is Sprite)
			{
//				TweenMax.to(e.target,0.2,{glowFilter:{alpha:0,strength:0}});
			}
		}

		public function get chooseRole():String
		{
			return _chooseRole;
		}

		public function set chooseRole(value:String):void
		{
			_chooseRole = value;
		}

		public function get choose():Player
		{
			return _choose;
		}

		public function getHeroName():String
		{
			return input.text;
		}
		
		//private
		private var _lastselected:Actor;
		private var _chooseRole:String;
		private var _textArea:TextArea;//游戏介绍
		private var _rolePanel:Panel;
		private var _choose:Player;
		private var allRoles:Dictionary;
		private var btnbegin:Button;
		private var act:TabBar;
		private var l:Label;
		private var input:InputText;
	}
}